package com.zhangkeyou.map.mapweb.domain.entity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.TableField;
import com.zhangkeyou.map.mapweb.mapper.handler.AttributesTypeHandler;
import lombok.Data;

import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;

@Data
public class Feature {
    private String id;

    private String featureId;
    private String featuresId;
    @JSONField(name = "attributes")
    @TableField(value = "attributes",typeHandler = AttributesTypeHandler.class)
    private Map<String,Object> attributes;

    //通过attributes设置parentId、name、id
    private String parentId;

    private String name;

    @JSONField(name = "geometry")
//        private Map<String,Object> geometry;
    private Geometry geometry;

    private Features childrenFeatures;

    @Override
    public String toString() {
        return "Feature{" +
                ", featureId='" + featureId + '\'' +
                ", name='" + name + '\'' +
                '}';
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }

    public void setAttributes(Map<String,Object> attributes){
        this.attributes=attributes;
    }

    public Map<String, Object> getAttributes() {
        return attributes;
    }

    @Data
    public static class Geometry{
        private static final DecimalFormat df = new DecimalFormat(".0000000");

        private List<List<double[]>> rings;


        public void setRings(List<List<double[]>> rings) {
            rings.forEach(ring->{
                ring.forEach(position->{
                    position[0]=Double.parseDouble(df.format(position[0]));
                    position[1]=Double.parseDouble(df.format(position[1]));
                });
            });
            this.rings = rings;
        }

        @Override
        public String toString(){
            String s = JSON.toJSONString(rings);
            return s;
        }
    }
}
